home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 114 / macaddict114.cdr / Software / Utilities / macam.0.8.4.dmg / macam sources / utilities / RGB888Scaler.m < prev    next >
Encoding:
Text File  |  2005-08-15  |  3.1 KB  |  113 lines

  1. /*
  2.  macam - webcam app and QuickTime driver component
  3.  Copyright (C) 2002 Matthias Krauss (macam@matthias-krauss.de)
  4.  
  5.  This program is free software; you can redistribute it and/or modify
  6.  it under the terms of the GNU General Public License as published by
  7.  the Free Software Foundation; either version 2 of the License, or
  8.  (at your option) any later version.
  9.  
  10.  This program is distributed in the hope that it will be useful,
  11.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  GNU General Public License for more details.
  14.  
  15.  You should have received a copy of the GNU General Public License
  16.  along with this program; if not, write to the Free Software
  17.  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  $Id: RGB888Scaler.m,v 1.2 2005/08/15 05:15:05 hxr Exp $
  19.  */
  20.  
  21. #import "RGB888Scaler.h"
  22. #include "GlobalDefs.h"
  23.  
  24.  
  25. @implementation RGB888Scaler
  26.  
  27. - (id) init {
  28.     self=[super init];
  29.     if (!self) return NULL;
  30.     dstWidth=0;
  31.     dstHeight=0;
  32.     dstData=NULL;
  33.     return self;
  34. }
  35.  
  36. - (void) dealloc 
  37. {
  38.     if (dstData) 
  39.     {
  40.         FREE(dstData,"RGB888Scaler dealloc dst data");
  41.         dstData=NULL;
  42.     }
  43.     
  44.     [super dealloc];
  45. }
  46.  
  47. - (BOOL) setDestinationWidth:(long)width height:(long)height {
  48.     if ((width<=0)||(height<=0)) return NO;
  49.     if ((width!=dstWidth)||(height!=dstHeight)) {     //Need a new dest buffer?
  50.         if (dstData) {
  51.             FREE(dstData,"RGB888Scaler resize dst data");
  52.             dstData=NULL;
  53.         }
  54.     }
  55.     dstWidth=width;
  56.     dstHeight=height;
  57.     if (!dstData) {
  58.         MALLOC(dstData,unsigned char*,dstWidth*dstHeight*3,"RGB888Scaler resize dst data");
  59.         NSAssert(dstData,@"Could not allocate a scaling buffer");
  60.     }
  61.     return (dstData!=NULL);
  62. }
  63.  
  64. - (long) destinationWidth { return dstWidth; }
  65.  
  66. - (long) destinationHeight { return dstHeight; }
  67.  
  68. - (long) destinationDataSize {
  69.     if (!dstData) return 0;
  70.     else return dstWidth*dstHeight*3;
  71. }
  72.  
  73. - (unsigned char*) convertSourceData:(unsigned char*)srcData width:(long)srcWidth height:(long)srcHeight {
  74.     long x,y,srcY,xSum,ySum,srcRowBytes,dstRowBytes;
  75.     unsigned char *srcRun,*dstRun;
  76.  
  77.     if ((srcWidth==dstWidth)&&(srcHeight==dstHeight)) {
  78.         return srcData;    //No scaling necessary
  79.     }
  80.     if (!dstData) return NULL;                        //Not set or no memory full
  81.  
  82.     dstRun=dstData;
  83.     xSum=0;
  84.     ySum=0;
  85.     srcY=0;
  86.     srcRowBytes=srcWidth*3;
  87.     dstRowBytes=dstWidth*3;
  88.     for (y=0;y<dstHeight;y++) {
  89. //Set src to start of line (dst will run through)
  90.         srcRun=srcData+srcRowBytes*srcY;    
  91. //Do inner (row) blit loop
  92.         xSum=0;
  93.         for (x=0;x<dstWidth;x++) {
  94. //Copy one pixel (too bad we're working on 24 bit - this is going to be slow)
  95.             dstRun[0]=srcRun[0];
  96.             dstRun[1]=srcRun[1];
  97.             dstRun[2]=srcRun[2];
  98.             dstRun+=3;
  99. //Update src pointer (Bresenham-style)
  100.             xSum+=srcWidth;
  101.             srcRun+=(xSum/dstWidth)*3;
  102.             xSum%=dstWidth;
  103.         }
  104. //Update y (Bresenham-style)
  105.         ySum+=srcHeight;
  106.         srcY+=ySum/dstHeight;
  107.         ySum%=dstHeight;
  108.     }
  109.     return dstData;
  110. }
  111.  
  112. @end
  113.